package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.mapper.CdzOrdersMapper;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.pojo.users.CdzGoods;
import com.zhentao.model.common.pojo.users.CdzOrders;
import com.zhentao.model.common.pojo.users.Users;
import com.zhentao.service.ICdzGoodsService;
import com.zhentao.service.ICdzOrdersService;
import com.zhentao.service.IUsersService;
import com.zhentao.userLocal.UserThreadLocalUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 兑换记录表 服务实现类
 * </p>
 *
 * @author mp
 * @since 2025-01-05
 */
@Service
public class CdzOrdersServiceImpl extends ServiceImpl<CdzOrdersMapper, CdzOrders> implements ICdzOrdersService {

    @Resource
    private IUsersService usersService;

    @Resource
    private ICdzGoodsService cdzGoodsService;

    @Override
    public ResponseResult ordersOne(Integer id) {
        //根据id查询这个id所属的商品
        CdzGoods cdzGoods = cdzGoodsService.getById(id);

        //商品所需积分
        Integer pointUse = cdzGoods.getPointUse();
        //获取用户id
        Long userId = UserThreadLocalUtil.getUser();
        //根据登录人的id进行查询登录人的兑换积分有多少
        Users user = usersService.getById(userId);
        // 该用户的积分
        Integer point = Integer.valueOf(user.getPoint());
        user.setPoint(String.valueOf(point-pointUse));
        //更新积分
        if (point-pointUse > 0) {
            usersService.updateById(user);
        }else {
            return ResponseResult.errorResult(500, "积分不足");
        }
        //减少库存
        cdzGoods.setNum(cdzGoods.getNum()-1);
        if (cdzGoods.getNum() > 0) {
            cdzGoodsService.updateById(cdzGoods);
        }else {
            return ResponseResult.errorResult(500, "库存不足");
        }
        // 添加兑换记录
        CdzOrders cdzOrders = new CdzOrders();
        cdzOrders.setUserId(userId);
        cdzOrders.setNum(1);
        cdzOrders.setPointXiao(pointUse);
        cdzOrders.setGoodsName(cdzGoods.getGoodsName());
        cdzOrders.setImg(cdzGoods.getImg());
        cdzOrders.setCreateBy(userId);
        cdzOrders.setCreateTime(LocalDateTime.now());
        boolean save = this.save(cdzOrders);
        if (!save) {
            return ResponseResult.errorResult(500, "添加失败");
        }
        return ResponseResult.okResult("添加成功");
    }

    @Override
    public ResponseResult getOneUserId() {
        //获取用户id
        Long userId = UserThreadLocalUtil.getUser();
        if (userId != null) {
            //根据用户id查询兑换记录
            LambdaQueryWrapper<CdzOrders> queryWrapper = Wrappers.<CdzOrders>lambdaQuery()
                    .eq(CdzOrders::getUserId, userId);
            if (list(queryWrapper) != null) {
                return ResponseResult.okResult(list(queryWrapper));
            }
        }
        return ResponseResult.errorResult(500, "暂无数据");
    }
}
